package com.yzjs.localhost.service;

import com.yzjs.common.util.conversion.DateUtils;
import com.yzjs.localhost.dao.tDatasourceSettings.TDatasourceSettingsMapper;
import com.yzjs.localhost.dao.tNeusoftStatistics.TNeusoftStatisticsMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.entity.tDatasourceSettings.TDatasourceSettingsEntity;
import com.yzjs.localhost.entity.tNeusoftBigDataTesting.TNeusoftBigDataTestingEntity;
import com.yzjs.localhost.entity.tNeusoftComplianceTesting.TNeusoftComplianceTestingEntity;
import com.yzjs.localhost.entity.tNeusoftStatistics.TNeusoftStatisticsEntity;
import com.yzjs.localhost.entity.tNeusoftUniformityTesting.TNeusoftUniformityTestingEntity;
import com.yzjs.localhost.service.tDataPersistenceAfter.TDataPersistenceAfterService;
import com.yzjs.localhost.service.tDataPersistenceBeforce.TDataPersistenceBeforceService;
import com.yzjs.localhost.service.tNeusoftBigDataTesting.TNeusoftBigDataTestingService;
import com.yzjs.localhost.service.tNeusoftComplianceTesting.TNeusoftComplianceTestingService;
import com.yzjs.localhost.service.tNeusoftUniformityTesting.TNeusoftUniformityTestingService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * 每日信息资源数据统计
 */
@Service("timingStatisticsService")
public class TimingStatisticsService {

    @Autowired
    private TDataPersistenceAfterService dataPersistenceAfterService ;

    @Autowired
    private TDataPersistenceBeforceService dataPersistenceBeforceService;

    // 合规性检测
    @Autowired
    private TNeusoftComplianceTestingService neusoftComplianceTestingService;

    // 一致性检测
    @Autowired
    private TNeusoftUniformityTestingService  neusoftUniformityTestingService;

    @Autowired
    private TNeusoftStatisticsMapper neusoftStatisticsMapper;

    @Autowired
    private TNeusoftBigDataTestingService  neusoftBigDataTestingService;


    @Autowired
    private TDatasourceSettingsMapper datasourceSettingsMapper ;


    /**
     *  每日资源信息统计量
     *  统计的行为，业务，一致性，合规性预警的数据量
     *  其中区块链上链量只能通过每日统计量来进行数据统计与前一天统计量作差，获取每日新增数据量
     */
    @Scheduled(cron = "0 */1 * * * *")
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public void TimingStatistics(){
        /**
         *  1.获取当天数据量，分别计算 行为,业务,一致性,合规预警数据量
         */
        Calendar calendar = DateUtils.getCalendar();
        TDataPersistenceBeforceEntity dataPersistenceBeforce = new TDataPersistenceBeforceEntity();
        dataPersistenceBeforce.setPageNum(1);
        dataPersistenceBeforce.setPageSize(10);
        dataPersistenceBeforce.setQueryStartTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,+1);
        dataPersistenceBeforce.setQueryEndTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        dataPersistenceBeforceService.findList(dataPersistenceBeforce);

        // 业务量，必须进行数据去重
        TDataPersistenceAfterEntity dataPersistenceAfter = new TDataPersistenceAfterEntity();
        dataPersistenceAfter.setPageNum(1);
        dataPersistenceAfter.setPageSize(10);
        calendar = DateUtils.getCalendar();
        dataPersistenceAfter.setQueryStartTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,+1);
        dataPersistenceAfter.setQueryEndTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        dataPersistenceAfterService.findList(dataPersistenceAfter);

        // 合规性检测
        TNeusoftComplianceTestingEntity neusoftComplianceTesting = new TNeusoftComplianceTestingEntity();
        neusoftComplianceTesting.setPageNum(1);
        neusoftComplianceTesting.setPageSize(10);
        calendar = DateUtils.getCalendar();
        neusoftComplianceTesting.setWarningTimeBegin(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,+1);
        neusoftComplianceTesting.setWarningTimeEnd(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        neusoftComplianceTestingService.findList(neusoftComplianceTesting);

        // 一致性检测
        TNeusoftUniformityTestingEntity neusoftUniformity = new TNeusoftUniformityTestingEntity();
        neusoftUniformity.setPageNum(1);
        neusoftUniformity.setPageSize(10);
        calendar = DateUtils.getCalendar();
        neusoftUniformity.setWarningTimeBegin(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,+1);
        neusoftUniformity.setWarningTimeEnd(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        neusoftUniformityTestingService.findList(neusoftUniformity);

        // 大数据合规风险检测
        TNeusoftBigDataTestingEntity neusoftBigData = new TNeusoftBigDataTestingEntity();
        neusoftBigData.setPageNum(1);
        neusoftBigData.setPageSize(10);
        calendar = DateUtils.getCalendar();
        neusoftBigData.setStartWarningTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,+1);
        neusoftBigData.setEndWarningTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        neusoftBigDataTestingService.findList(neusoftBigData);

        // 前一日获取所有数据量
        TNeusoftStatisticsEntity tNeusoftStatistics = new TNeusoftStatisticsEntity();
        calendar = DateUtils.getCalendar();
        calendar.add(Calendar.DATE,-1);
        tNeusoftStatistics.setQueryEndTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        List<TNeusoftStatisticsEntity> tNeusoftStatisticsList = neusoftStatisticsMapper.findList(tNeusoftStatistics);
        // 数据存证留痕
        Integer onChain = tNeusoftStatisticsList.stream().mapToInt(TNeusoftStatisticsEntity::getOnChain).sum();
        // 链上数据新增
        Integer onChainAdd = tNeusoftStatisticsList.stream().mapToInt(TNeusoftStatisticsEntity::getOnChainAdd).sum();
        // 链上数据修改
        Integer onChainUpdate = tNeusoftStatisticsList.stream().mapToInt(TNeusoftStatisticsEntity::getOnChainUpdate).sum();
        // 链上数据删除
        Integer onChainDelete = tNeusoftStatisticsList.stream().mapToInt(TNeusoftStatisticsEntity::getOnChainDelete).sum();

//        TDatasourceSettingsEntity settingsEntity  = new TDatasourceSettingsEntity();
//        settingsEntity.setTableName("t_neusoft_uniformity_testing");
//        TDatasourceSettingsEntity  uniformitySettings  = datasourceSettingsMapper.findOne(settingsEntity);
//
//
//        settingsEntity.setTableName("t_neusoft_compliance_testing");
//        TDatasourceSettingsEntity  complianceSettings  = datasourceSettingsMapper.findOne(settingsEntity);

        TDataPersistenceBeforceEntity dataPersistenceBeforceSource = new TDataPersistenceBeforceEntity();
        List<TDataPersistenceBeforceEntity>  beforcelist  =  dataPersistenceBeforceService.findList(dataPersistenceBeforceSource);

        onChain = ((Long)beforcelist.stream().count()).intValue()  -onChain;
        onChainAdd =((Long)beforcelist.stream().filter(x->x.getOperatorCode().contains("add")).count()).intValue()-onChainAdd;
        onChainUpdate= ((Long)beforcelist.stream().filter(x->x.getOperatorCode().contains("edit")).count()).intValue()-onChainUpdate;
        onChainDelete= ((Long)beforcelist.stream().filter(x->!x.getOperatorCode().contains("add")&& !x.getOperatorCode().contains("edit") ).count()).intValue()-onChainDelete;

        tNeusoftStatistics = new TNeusoftStatisticsEntity();
        calendar = DateUtils.getCalendar();
        tNeusoftStatistics.setQueryStartTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,+1);
        tNeusoftStatistics.setQueryEndTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));

        TNeusoftStatisticsEntity tNeusoftStatisticResult  = neusoftStatisticsMapper.findOne(tNeusoftStatistics);
        if (tNeusoftStatisticResult == null){
            tNeusoftStatisticResult = new TNeusoftStatisticsEntity();
            calendar = DateUtils.getCalendar();
            tNeusoftStatisticResult.setDateInfo(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        }
        tNeusoftStatisticResult.setBusinessDataCount(dataPersistenceBeforce.getTotalRowCount().intValue());
        tNeusoftStatisticResult.setBehaviorDataCount(dataPersistenceAfter.getTotalRowCount().intValue());
        tNeusoftStatisticResult.setUniformityDataCount(neusoftUniformity.getTotalRowCount().intValue());
        tNeusoftStatisticResult.setComplianceDataCount(neusoftComplianceTesting.getTotalRowCount().intValue());
        tNeusoftStatisticResult.setBigDataVerification(neusoftBigData.getTotalRowCount().intValue());
        tNeusoftStatisticResult.setOnChain(onChain);
        tNeusoftStatisticResult.setOnChainAdd(onChainAdd);
        tNeusoftStatisticResult.setOnChainUpdate(onChainUpdate);
        tNeusoftStatisticResult.setOnChainDelete(onChainDelete);
        if (tNeusoftStatisticResult.getId() == null){
            neusoftStatisticsMapper.insert(tNeusoftStatisticResult);
        }else {
            neusoftStatisticsMapper.update(tNeusoftStatisticResult);
        }
    }
}
